class_name ControllerComponent
extends BaseComponent

@export var is_face_right:bool = true
var target_vel:Vector2 = Vector2.ZERO

func init_component(entity):
	super.init_component(entity)
func flip():
	if "velocity" in cur_entity:
		var vel:Vector2 = cur_entity.velocity
		if (vel.x > 0.001 and cur_entity.scale.y < 0) or (vel.x < -0.001 and cur_entity.scale.y > 0):
			cur_entity.rotate(PI)
			cur_entity.apply_scale(Vector2(1,-1))
func get_target_vel() -> Vector2:
	return target_vel
func set_target_vel(delta:float):
	pass
func get_animation(anim_name:String) -> Animation:
	var anim:Animation = cur_entity.animation_player.get_animation(anim_name)
	return anim if anim else null
func play_animation(anim_name:String,speed:float = 1):
	cur_entity.animation_player.play(anim_name,-1,speed)
func set_animation_speed_scale(scale:float):
	cur_entity.animation_player.speed_scale *= scale
func add_anim_finished_event(anim_name:String,callback:Callable,connect_flag:ConnectFlags = 0):
	var anim:Animation = cur_entity.animation_player.get_animation(anim_name)
	if anim and not cur_entity.animation_player.animation_finished.is_connected(callback):
		cur_entity.animation_player.animation_finished.connect(callback,connect_flag)
func remove_anim_finished_event(anim_name:String,callback:Callable):
	var anim:Animation = cur_entity.animation_player.get_animation(anim_name)
	if anim and cur_entity.animation_player.animation_finished.is_connected(callback):
		cur_entity.animation_player.animation_finished.disconnect(callback)
	
func _init() -> void:
	component_name = "ControllerComponent"
	priority = 5 if priority == 0 else priority
func _process(delta: float) -> void:
	if not inited or not GameManager.get_cur_game_state() == GameEnum.GameState.Gaming:
		return
	flip()
func _physics_process(delta: float) -> void:
	set_target_vel(delta)
